package com.studorm.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.studorm.Connection_mysql;

public class RegisterAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4023883726735343388L;

	private List<String> options = new ArrayList<String>();

	public List<String> getOptions() {
		return options;
	}

	public void setOptions(List<String> options) {
		this.options = options;
	}
	
	public String loadDepartment() throws Exception{
		Connection conn = Connection_mysql.getConnection();
		String sql = "SELECT distinct department FROM studormcms.department;";
		Statement stat = conn.createStatement();
		ResultSet res = stat.executeQuery(sql);

		while(res.next()){
			options.add(res.getString(1));
		}
		res.close();
		stat.close();
		conn.close();

		return SUCCESS;
	}

	public String loadMajorByDepartment() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		String department = request.getParameter("department");
		if(department == null || department.isEmpty()){
			return ERROR;
		}
		String sql = "SELECT DISTINCT major FROM department WHERE department = ?;";
		
		Connection conn = Connection_mysql.getConnection();
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, department);
		ResultSet res = ps.executeQuery();
		while(res.next()){
			options.add(res.getString(1));
		}
		
		res.close();
		ps.close();
		conn.close();
		
		return SUCCESS;
	}
	
	public String loadGrade() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String department = request.getParameter("department");
		if(department == null || department.isEmpty()){
			return ERROR;
		}
		
		String major = request.getParameter("major");
		if(major == null || major.isEmpty()){
			return ERROR;
		}
		
		String sql = "SELECT DISTINCT grade FROM department WHERE department = ? AND major = ?;";
		
		Connection conn = Connection_mysql.getConnection();
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, department);
		ps.setString(2, major);
		ResultSet res = ps.executeQuery();
		while(res.next()){
			options.add(res.getString(1));
		}
		
		res.close();
		ps.close();
		conn.close();
		
		return SUCCESS;
	}
	
	public String register() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		
		Pattern patternNu = Pattern.compile("\\d{8}");// 匹配8位整数
		
		String studentNu = request.getParameter("studentNu");
		if(studentNu == null || studentNu.isEmpty() || !patternNu.matcher(studentNu).matches()){
			return ERROR;
		}
		
		String password = request.getParameter("password");
		if(password == null || password.isEmpty()){
			return ERROR;
		}
		
		String name = request.getParameter("name");
		if(name == null || name.isEmpty()){
			return ERROR;
		}
		
		String sex = request.getParameter("sex");
		if(sex == null || sex.isEmpty()){
			return ERROR;
		}
		
		String department = request.getParameter("department");
		if(department == null || department.isEmpty()){
			return ERROR;
		}
		
		String major = request.getParameter("major");
		if(major == null || major.isEmpty()){
			return ERROR;
		}
		
		String grade = request.getParameter("grade");
		if(grade == null || grade.isEmpty()){
			return ERROR;
		}
		
		String bdName = request.getParameter("bdName"); // dormBuildingName
		if(bdName == null || bdName.isEmpty()){
			return ERROR;
		}
		
		String dormNu = request.getParameter("dormNu");
		if(dormNu == null || dormNu.isEmpty()){
			return ERROR;
		}
		
		String contactInf = request.getParameter("contactInf");
		if(contactInf == null || contactInf.isEmpty()){
			return ERROR;
		}

		String sql = "SELECT id FROM dorminf WHERE dormBdName = ? and dormNu = ?";
		Connection conn = Connection_mysql.getConnection();
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, bdName);
		ps.setString(2, dormNu);
		ResultSet res = ps.executeQuery();
		
		int dormId = 0;
		while(res.next()){
			dormId = res.getInt(1);
		}
		res.close();
		ps.close();
		
		if(dormId == 0){
			return ERROR;
		}
		
		sql = "SELECT id FROM department WHERE department = ? and major = ? and grade=?;";
		ps = conn.prepareStatement(sql);
		ps.setString(1, department);
		ps.setString(2, major);
		ps.setString(3, grade);
		res = ps.executeQuery();
		
		int departmentId = 0;
		while(res.next()){
			departmentId = res.getInt(1);
		}
		res.close();
		ps.close();
		
		if(departmentId == 0){
			return ERROR;
		}
		
		sql = "INSERT INTO studentsinf (studentNu, password, name, sex, contactInf, dorm, department) VALUES (?, MD5(?), ?, ?, ?, ?, ?);";
		ps = conn.prepareStatement(sql);
		ps.setString(1, studentNu);
		ps.setString(2, password);
		ps.setString(3, name);
		ps.setString(4, sex);
		ps.setString(5, contactInf);
		ps.setInt(6, dormId);
		ps.setInt(7, departmentId);
		
		int count = ps.executeUpdate();
		ps.close();
		conn.close();
		
		if(count == 0){
			return ERROR;
		}else{
			return SUCCESS;
		}
	}
}









